package com.tinyseed.emcp.api.basicdata.rest;

import com.tinyseed.emcp.api.basicdata.model.EnterpriseInputDTO;
import com.tinyseed.emcp.api.basicdata.model.EnterpriseOutputDTO;
import com.tinyseed.emcp.api.basicdata.service.EnterpriseService;
import com.tinyseed.hmxx.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController {

    private final EnterpriseService enterpriseService;

    public EnterpriseController(EnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    @GetMapping(value = "/list")
    public ResponseEntity list(int size, int current) {
        return new ResponseEntity<>(enterpriseService.getList(current, size), HttpStatus.OK);

    }

    @GetMapping(value = "/listAll")
    @ResponseBody
    public ResponseEntity list(EnterpriseInputDTO request, int limit, int page) {
        Page list = enterpriseService.getList(request, limit, page);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @GetMapping(value = "/optional")
    @ResponseBody
    public ResponseEntity option() {
        List list = enterpriseService.getOption();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }


    /**
     * 新增
     */
    @PostMapping(value = "/save")
    @ResponseBody
    public ResponseEntity save(@RequestBody EnterpriseInputDTO request) {
        EnterpriseOutputDTO enterpriseOutputDTO = enterpriseService.save(request);
        return new ResponseEntity<>(enterpriseOutputDTO, HttpStatus.OK);

    }

    /**
     * 修改
     */
    @PostMapping(value = "/update")
    @ResponseBody
    public ResponseEntity update(@RequestBody EnterpriseInputDTO request) {
        EnterpriseOutputDTO enterpriseOutputDTO = enterpriseService.update(request);
        return new ResponseEntity<>(enterpriseOutputDTO, HttpStatus.OK);

    }

    @PostMapping(value = "/get")
    @ResponseBody
    public ResponseEntity get(Long id) {
        EnterpriseOutputDTO enterpriseOutputDTO = enterpriseService.getDataById(id);
        return new ResponseEntity<>(enterpriseOutputDTO, HttpStatus.OK);

    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public ResponseEntity delete(Long id) {
        enterpriseService.delete(id);
        return new ResponseEntity<>( HttpStatus.OK);

    }
}
